/*
    Copyright 2011 Peter Pratscher
 
    This file is part of Surveillance.NET.

    Surveillance.NET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Surveillance.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Surveillance.NET. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net.Mail;
using System.Runtime.Serialization;

namespace SurveillanceDotNet.Model
{
    [Serializable]
    public class EMailAlarmAction : AlarmAction
    {
        public MailAccount EMailAccount { get; set; }
        public MailAddress ToAddress { get; set; }
        public string Subject { get; set; }
        public string MessageText { get; set; }
        public bool AttachCameraPictures { get; set; }

        public EMailAlarmAction(MailAccount eMailAccount, MailAddress toAddress, string subject, string messageText, bool attachCameraPictures)
            : base()
        {
            EMailAccount = eMailAccount;
            ToAddress = toAddress;
            Subject = subject;
            MessageText = messageText;
            AttachCameraPictures = attachCameraPictures;

            Status = "Not active";
        }

        public override void Action()
        {
            Status = "Active";

            List<Attachment> attachments = new List<Attachment>();

            if (AttachCameraPictures)
            {
                for (int i = 0; i < _Database.Cameras.Count; i++)
                {
                    Image bmp = _Database.Cameras[i].CurrentFrame;
                    MemoryStream stream = new MemoryStream();
                    bmp.Save(stream, ImageFormat.Jpeg);
                    stream.Position = 0;

                    attachments.Add(new System.Net.Mail.Attachment(stream, "camera" + i + ".jpeg"));
                }
            }

            EMailAccount.SendMail(ToAddress, Subject, MessageText, attachments);

            Status = "Not active";
        }

        #region ISerializable Implementation
        protected EMailAlarmAction(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            EMailAccount = (MailAccount)info.GetValue("EMailAccount", typeof(MailAccount));
            ToAddress = new MailAddress(info.GetString("ToAddressAdress"), info.GetString("ToAddressDisplayName"));
            Subject = info.GetString("Subject");
            MessageText = info.GetString("MessageText");
            AttachCameraPictures = info.GetBoolean("AttachCameraPictures");

            Status = "Not active";
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("EMailAccount", EMailAccount);
            info.AddValue("ToAddressAdress", ToAddress.Address);
            info.AddValue("ToAddressDisplayName", ToAddress.DisplayName);
            info.AddValue("Subject", Subject);
            info.AddValue("MessageText", MessageText);
            info.AddValue("AttachCameraPictures", AttachCameraPictures);
        }
        #endregion
    }
}
